package com.eims.sls_app.ui;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.Selection;
import android.text.Spannable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.VolleyError;
import bls.juejin.cn.R;
import com.eims.sls_app.dataaccess.SelectedNumbers;
import com.eims.sls_app.ui.adapter.MyBetLotteryListAdapterFC3D;
import com.eims.sls_app.ui.adapter.MyGridViewAdapterFC3D;
import com.eims.sls_app.utils.App;
import com.eims.sls_app.utils.App;
import com.eims.sls_app.utils.LotteryUtils;
import com.eims.sls_app.utils.NumberTools;
import com.eims.sls_app.utils.RequestUtil;
import com.eims.sls_app.view.ConfirmDialog;
import com.eims.sls_app.view.MyListView2;
import com.eims.sls_app.view.MyToast;

import org.json.JSONObject;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/** 功能：3D投注页面 版 */
public class BetActivityFC3D extends MyBaseActivity implements OnClickListener {
	private Context context = BetActivityFC3D.this;
	private final static String TAG = "BetActivityFC3D";

	/* 头部 */
	private RelativeLayout layout_top_select;// 顶部布局
	private ImageButton btn_back; // 返回
	private ImageView iv_up_down;// 玩法提示图标
	private Button btn_playtype;// 玩法
	private ImageButton btn_help;// 帮助
	private ConfirmDialog dialog;// 提示框

	/* 尾部 */
	private Button btn_follow; // 发起合买
	private Button btn_clearall; // 清除全部
	private Button btn_submit; // 付款
	public TextView tv_tatol_count;// 总注数
	public TextView tv_tatol_money;// 总钱数

	private LinearLayout btn_continue_select;// 继续投注
	private LinearLayout btn_automatic_select;// 机选1注
	private ImageView bet_btn_deleteall;// 清空
	private TextView bet_tv_guize; // 委托投注规则

	private MyListView2 bet_lv_scheme;// 自定义listview
	public MyBetLotteryListAdapterFC3D adapter;
	private Intent intent;
	private List<String> list_bai, list_shi, list_ge, list_hezhi,
			list_zixuanhezhi, list_daxiao, list_jiou;
	private boolean isCaist, isTuolaji;
	private long sumCount, totalMoney; // 方案总注数 // 方案总金额
	private EditText et_bei, et_qi; // 用户输入的倍数 // 用户输入的期数
	private boolean flag;
	private CheckBox bet_cb_stopfollow;
	private int isStopChase = 1;
	private boolean backHome = false;

	private DecimalFormat format = new DecimalFormat("#####0.00");

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		// 设置无标题
		this.requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_bet);
		App.activityS.add(this);
		App.activityS1.add(this);
		if (App.list_numbers == null)
			App.list_numbers = new ArrayList<SelectedNumbers>();
		findView();
		setListener();
		changeTextShow();
		init();
	}

	/** 初始化UI */
	private void findView() {
		btn_back = (ImageButton) findViewById(R.id.btn_back);
		btn_playtype = (Button) findViewById(R.id.btn_playtype);
		btn_help = (ImageButton) findViewById(R.id.btn_help);
		iv_up_down = (ImageView) findViewById(R.id.iv_up_down);
		btn_clearall = (Button) findViewById(R.id.btn_clearall);
		btn_submit = (Button) findViewById(R.id.btn_submit);
		btn_continue_select = (LinearLayout) this
				.findViewById(R.id.btn_continue_select);
		btn_automatic_select = (LinearLayout) this
				.findViewById(R.id.btn_automatic_select);
		bet_btn_deleteall = (ImageView) this
				.findViewById(R.id.bet_btn_deleteall);
		btn_follow = (Button) this.findViewById(R.id.btn_follow);
		et_bei = (EditText) this.findViewById(R.id.et_bei);
		et_qi = (EditText) this.findViewById(R.id.et_qi);

		tv_tatol_count = (TextView) this.findViewById(R.id.tv_tatol_count);
		tv_tatol_money = (TextView) this.findViewById(R.id.tv_tatol_money);
		bet_cb_stopfollow = (CheckBox) this
				.findViewById(R.id.bet_cb_stopfollow);
		bet_tv_guize = (TextView) this.findViewById(R.id.bet_tv_guize);

		bet_lv_scheme = (MyListView2) this.findViewById(R.id.bet_lv_scheme);

		btn_playtype.setText("福彩3D投注单");

		adapter = new MyBetLotteryListAdapterFC3D(BetActivityFC3D.this,
				App.list_numbers);
		if (App.list_numbers.size() != 0) {
			Log.i(TAG, "adapter中的元素数量:::::::::"
					+ App.list_numbers.get(0).getLotteryNumber());

		}
		int nums = adapter.getCount();
		Log.i(TAG, "adapter中的元素数量" + nums);
		for (int i = 0; i < adapter.getCount(); i++) {
			SelectedNumbers item = (SelectedNumbers) adapter.getItem(i);
			Log.i(TAG, "adapter中的元素:::" + item.toString());
		}
		// 隐藏与显示
		btn_help.setVisibility(View.GONE);
		btn_clearall.setVisibility(View.GONE);
		iv_up_down.setVisibility(View.GONE);
		btn_follow.setVisibility(View.VISIBLE);
	}

	/** 初始化属性 */
	private void init() {
		if (MyGridViewAdapterFC3D.playType == 601 && checkVisiBle()) {
			btn_automatic_select.setVisibility(View.VISIBLE);
		} else {
			btn_automatic_select.setVisibility(View.INVISIBLE);
		}
		boolean isFlag = (null != MyGridViewAdapterFC3D.baiSet && MyGridViewAdapterFC3D.baiSet
				.size() != 0)
				|| (null != MyGridViewAdapterFC3D.hezhiSet && MyGridViewAdapterFC3D.hezhiSet
						.size() != 0)
				|| (null != MyGridViewAdapterFC3D.zixuanhezhiSet && MyGridViewAdapterFC3D.zixuanhezhiSet
						.size() != 0)
				|| (null != MyGridViewAdapterFC3D.daxiaoSet && MyGridViewAdapterFC3D.daxiaoSet
						.size() != 0)
				|| (null != MyGridViewAdapterFC3D.jiouSet && MyGridViewAdapterFC3D.jiouSet
						.size() != 0)
				|| MyGridViewAdapterFC3D.isCaist
				|| MyGridViewAdapterFC3D.isTuolaji
				|| (null != MyGridViewAdapterFC3D.shiSet && MyGridViewAdapterFC3D.shiSet
						.size() != 0)
				|| (null != MyGridViewAdapterFC3D.geSet && MyGridViewAdapterFC3D.geSet
						.size() != 0);
		if (isFlag) {

			SelectedNumbers numbers = new SelectedNumbers();

			// 保存cp号码
			String lotteryNumberHS = "";
			String lotteryNumber = " ";
			if (MyGridViewAdapterFC3D.playType == 610) {// 和数
				for (String st : MyGridViewAdapterFC3D.hezhiSet) {
					lotteryNumber += st + " ";
					lotteryNumberHS += st + ",";
				}
				lotteryNumber = lotteryNumber.substring(0,
						lotteryNumber.length() - 1);
				lotteryNumberHS = lotteryNumberHS.substring(0,
						lotteryNumberHS.length() - 1);
			} else {
				for (String st : MyGridViewAdapterFC3D.baiSet) {
					lotteryNumber += st;
				}

				if ((MyGridViewAdapterFC3D.playType == 604 && MyGridViewAdapterFC3D.baiSet
						.size() == 0)
						|| (MyGridViewAdapterFC3D.playType == 606 && MyGridViewAdapterFC3D.baiSet
								.size() == 0)) {
					lotteryNumber += "*,";
				} else
					lotteryNumber += ",";
				for (String st : MyGridViewAdapterFC3D.shiSet) {
					lotteryNumber += st;
				}
			}
			if ((MyGridViewAdapterFC3D.playType == 604 && MyGridViewAdapterFC3D.shiSet
					.size() == 0)
					|| (MyGridViewAdapterFC3D.playType == 606 && MyGridViewAdapterFC3D.shiSet
							.size() == 0)) {
				lotteryNumber += "*,";
			} else
				lotteryNumber += ",";
			for (String st : MyGridViewAdapterFC3D.geSet) {
				lotteryNumber += st;
			}
			if ((MyGridViewAdapterFC3D.playType == 604 && MyGridViewAdapterFC3D.geSet
					.size() == 0)
					|| (MyGridViewAdapterFC3D.playType == 606 && MyGridViewAdapterFC3D.geSet
							.size() == 0)) {
				lotteryNumber += "*";
			}
			lotteryNumber = lotteryNumber.trim();
			numbers.setPlayType(MyGridViewAdapterFC3D.playType);
			if (lotteryNumber.contains(",")) {
				String show = lotteryNumber;
				if (lotteryNumber.contains(" ")) {
					show = lotteryNumber.replace(" ", "");
				}
				String[] array = show.split(",");
				StringBuilder num = new StringBuilder();
				if (1 == array.length) {
					numbers.setShowLotteryNumber(lotteryNumber
							.replace("|", " ").replace("*", "-")
							.replace(",", " "));
				} else {
					for (int i = 0; i < array.length; i++) {
						if (i == array.length - 1) {
							num.append(array[i]);
						} else {
							num.append(array[i]).append(" ");
						}
					}
					numbers.setShowLotteryNumber(num.toString()
							.replace("|", " ").replace("*", "-")
							.replace(",", " "));
				}
			}
			if (MyGridViewAdapterFC3D.playType > 601
					&& MyGridViewAdapterFC3D.playType < 604) {
				lotteryNumber = lotteryNumber.replace(",", "");
				char a[] = lotteryNumber.toCharArray();
				String kk = "";
				for (char anA : a) {
					kk = kk + anA + ",";
				}
				kk = kk.substring(0, kk.length() - 1);
				kk.trim();
				if (kk.contains("|")) {
					kk = kk.replace("|", ",");// 将“|”替换成“，”
				}
				numbers.setLotteryNumber(kk);
			} else if (MyGridViewAdapterFC3D.playType == 610) {
				String num1 = NumberTools.lotteryNumberFormatConvert(
						numbers.getPlayType(), lotteryNumberHS);
				if (num1.contains("|")) {
					num1 = num1.replace("|", ",");// 将“|”替换成“，”
				}
				numbers.setLotteryNumber(num1);
			} else if (MyGridViewAdapterFC3D.playType == 605
					|| MyGridViewAdapterFC3D.playType == 607
					|| MyGridViewAdapterFC3D.playType == 608) {
				lotteryNumber = lotteryNumber.substring(0,
						lotteryNumber.length() - 2);
				String lotteryNumberNew = "";
				for (int i = 0; i < lotteryNumber.length(); i++) {
					lotteryNumberNew += lotteryNumber.charAt(i) + ",";
				}
				String num1 = lotteryNumberNew.substring(0,
						lotteryNumberNew.length() - 1);
				if (num1.contains("|")) {
					num1 = num1.replace("|", ",");// 将“|”替换成“，”
				}
				numbers.setLotteryNumber(num1);
			} else if (MyGridViewAdapterFC3D.playType == 611) {
				String num1 = lotteryNumber.replace(" ", "").trim();
				if (num1.contains("|")) {
					num1 = num1.replace("|", ",");// 将“|”替换成“，”
				}
				numbers.setLotteryNumber(num1);
			} else {
				String num1 = lotteryNumber.trim();
				if (num1.contains("|")) {
					num1 = num1.replace("|", ",");// 将“|”替换成“，”
				}
				numbers.setLotteryNumber(num1);
			}
			if (MyGridViewAdapterFC3D.playType == 613) {// 猜大小
				lotteryNumber = "";
				for (String st : MyGridViewAdapterFC3D.daxiaoSet) {
					lotteryNumber += st;
				}
				numbers.setShowLotteryNumber(lotteryNumber.trim());
				String num1 = lotteryNumber.trim();
				if (num1.contains("|")) {
					num1 = num1.replace("|", ",");// 将“|”替换成“，”
				}
				numbers.setLotteryNumber(num1);
			}
			if (MyGridViewAdapterFC3D.playType == 614) {// 猜三同
				if (MyGridViewAdapterFC3D.isCaist) {
					numbers.setShowLotteryNumber("三同号");
					numbers.setLotteryNumber("三同号");
				}
			}
			if (MyGridViewAdapterFC3D.playType == 616) {// 猜奇偶
				lotteryNumber = "";
				for (String st : MyGridViewAdapterFC3D.jiouSet) {
					lotteryNumber += st;
				}
				numbers.setShowLotteryNumber(lotteryNumber.trim());
				numbers.setLotteryNumber(lotteryNumber.trim());
			}
			if (MyGridViewAdapterFC3D.playType == 615) {// 拖拉机
				if (MyGridViewAdapterFC3D.isTuolaji) {
					numbers.setShowLotteryNumber("拖拉机");
					numbers.setLotteryNumber("拖拉机");
				}
			}
			numbers.setCount(App.totalCount);
			numbers.setMoney(App.totalCount * 2);
//			Log.i(TAG + "-->init()",
//					"3D-->ShowLotteryNumber==" + numbers.getShowLotteryNumber());
//			Log.i(TAG + "-->init()",
//					"3D-->LotteryNumber==" + numbers.getLotteryNumber());
			App.list_numbers.add(0, numbers);
			adapter.notifyDataSetChanged();
		}
		isStopChase = 1;
		changeTextShow();
		btn_submit.setText("付款");
		dialog = new ConfirmDialog(this, R.style.dialog);
	}

	/** 得到玩法ID */
	private int getPlayType(String lotteryId, int playType) {
		if ("6".equals(lotteryId)) {
			switch (playType) {
			case 1:
				playType = 601;
				break;
			case 2:
				playType = 602;
				break;
			case 3:
				playType = 604;
				break;
			case 4:
				playType = 603;
				break;
			case 5:
				playType = 605;
				break;
			}
		} else {
			switch (playType) {
			case 1:
				playType = 6301;
				break;
			case 2:
				playType = 6302;
				break;
			case 3:
				playType = 6304;
				break;
			case 4:
				playType = 6303;
				break;
			case 5:
				playType = 6305;
				break;
			}
		}
		return playType;
	}

	/** 设置监听 */
	private void setListener() {
		btn_continue_select.setOnClickListener(this);
		btn_automatic_select.setOnClickListener(this);
		bet_btn_deleteall.setOnClickListener(this);
		btn_submit.setOnClickListener(this);
		btn_follow.setOnClickListener(this);
		btn_back.setOnClickListener(this);

		bet_lv_scheme.setAdapter(adapter);
		bet_lv_scheme.setOnItemClickListener(new listItemClick());

		et_bei.addTextChangedListener(bei_textWatcher);
		et_qi.addTextChangedListener(qi_textWatcher);
		bet_tv_guize.setOnClickListener(this);

		bet_cb_stopfollow.setOnCheckedChangeListener(new MyCheckChange());
	}

	/** 复选框 **/
	class MyCheckChange implements OnCheckedChangeListener {
		@Override
		public void onCheckedChanged(CompoundButton buttonView,
				boolean isChecked) {
			if (isChecked)
				isStopChase = 1;
			else
				isStopChase = 0;
		}
	}

	private boolean checkVisiBle() {
		if (App.list_numbers.size() > 0) {
			for (int i = 0; i < App.list_numbers.size(); i++) {
				if (App.list_numbers.get(i).getPlayType() > 601)
					return false;
			}
		}
		return true;
	}

	/** listView 的 item 点击监听 */
	class listItemClick implements OnItemClickListener {
		@Override
		public void onItemClick(AdapterView<?> arg0, View arg1, int position,
				long arg3) {

			SelectedNumbers num = App.list_numbers.get(position);
			// 跳转页面并传值
			intent = new Intent(BetActivityFC3D.this,
					SelectNumberActivityFC3D.class);
			Bundle bundle = new Bundle();
			bundle.putInt("type", num.getPlayType());
			Log.i("x", "listItemClick==>type  " + num.getPlayType());

			App.totalCount = num.getCount();
			Log.i("x",
					"listItemClick==>lotteryNumbers  "
							+ num.getShowLotteryNumber());
			String[] lotteryNumbers = num.getShowLotteryNumber().trim()
					.replace("\\|", " ").split(" ");

			if (num.getPlayType() == 601) {
				if (lotteryNumbers.length != 3) {
					lotteryNumbers = lotteryNumbers[0].split(",");
				}
			}
			for (int i = 0; i < lotteryNumbers.length; i++) {
				Log.i(TAG, "=========" + lotteryNumbers[i]);
			}

			clearSelectNumber();
			if (num.getPlayType() == 610) {
				Collections.addAll(list_hezhi, lotteryNumbers);
			}

			if (lotteryNumbers.length == 3) {
				for (int i = 0; i < lotteryNumbers.length; i++) {
					for (char str : lotteryNumbers[i].toCharArray()) {
						if (i == 0) {
							list_bai.add(str + "");
						}
						if (i == 1) {
							list_shi.add(str + "");
						}
						if (i == 2) {
							list_ge.add(str + "");
						}
					}
				}
			} else {
				if (lotteryNumbers.length > 0) {

					for (String lotteryNumber : lotteryNumbers) {
						for (char str : lotteryNumber.toCharArray()) {
							list_bai.add(str + "");
						}
					}

				}
			}
			if (num.getPlayType() == 613)// 猜大小
			{
				if (lotteryNumbers.length > 0) {
					list_daxiao.add(lotteryNumbers[0]);
				}
			}
			if (num.getPlayType() == 616)// 猜奇偶
			{
				if (lotteryNumbers.length > 0) {
					list_jiou.add(lotteryNumbers[0]);
				}
			}
			if (num.getPlayType() == 614) {// 猜三同
				isCaist = lotteryNumbers.length > 0;
			}
			if (num.getPlayType() == 615) {// 拖拉机
				isTuolaji = lotteryNumbers.length > 0;
			}
			bundle.putStringArrayList("bai", (ArrayList<String>) list_bai);
			bundle.putStringArrayList("shi", (ArrayList<String>) list_shi);
			bundle.putStringArrayList("ge", (ArrayList<String>) list_ge);
			bundle.putStringArrayList("hezhi", (ArrayList<String>) list_hezhi);
			bundle.putStringArrayList("zixuanhezhi",
					(ArrayList<String>) list_zixuanhezhi);
			bundle.putStringArrayList("daxiao", (ArrayList<String>) list_daxiao);
			bundle.putStringArrayList("jiou", (ArrayList<String>) list_jiou);
			bundle.putBoolean("santong", isCaist);
			bundle.putBoolean("tuolaji", isTuolaji);
			// 将Bundle 放入Intent
			intent.putExtra("bundle", bundle);
			Log.i("x", "listItemClick==>bundle  " + bundle.toString());
			BetActivityFC3D.this.startActivity(intent);
			App.list_numbers.remove(position);

		}

		private void clearSelectNumber() {
			if (null == list_bai) {
				list_bai = new ArrayList<String>();
			} else {
				list_bai.clear();
			}
			if (null == list_shi) {
				list_shi = new ArrayList<String>();
			} else {
				list_shi.clear();
			}
			if (null == list_ge) {
				list_ge = new ArrayList<String>();
			} else {
				list_ge.clear();
			}
			if (null == list_hezhi) {
				list_hezhi = new ArrayList<String>();
			} else {
				list_hezhi.clear();
			}
			if (null == list_zixuanhezhi) {
				list_zixuanhezhi = new ArrayList<String>();
			} else {
				list_zixuanhezhi.clear();
			}
			if (null == list_daxiao) {
				list_daxiao = new ArrayList<String>();
			} else {
				list_daxiao.clear();
			}
			if (null == list_jiou) {
				list_jiou = new ArrayList<String>();
			} else {
				list_jiou.clear();
			}
		}
	}

	/** 当文本的值改变时 */
	private TextWatcher qi_textWatcher = new TextWatcher() {
		@Override
		public void onTextChanged(CharSequence arg0, int arg1, int arg2,
				int arg3) {
		}

		@Override
		public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
				int arg3) {
		}

		@Override
		public void afterTextChanged(Editable edt) {
			if (edt.toString().trim().length() != 0) {
				if (Integer.parseInt(edt.toString().trim()) > App.lottery
						.getDtCanChaseIsuses().size()) {
					et_qi.setText(App.lottery.getDtCanChaseIsuses().size()
							+ "");
					et_qi.setSelection(et_qi.getText().length());
					MyToast.getToast(
							getApplicationContext(),
							"最多可追"
									+ App.lottery.getDtCanChaseIsuses()
											.size() + "期").show();
				}
				if (Integer.parseInt(edt.toString().trim()) == 0) {

					et_qi.setText("1");
					et_qi.setSelection(et_qi.getText().length());
					MyToast.getToast(getApplicationContext(), "至少可追1期").show();
				}
				if (edt.toString().substring(0, 1).equals("0")) {
					et_qi.setText(edt.toString().subSequence(1,
							edt.toString().length()));
					et_qi.setSelection(0);
				}
			}
			changeTextShow();
			setCursorPosition(et_qi);
		}
	};

	/** 当文本的值改变时 */
	private TextWatcher bei_textWatcher = new TextWatcher() {
		@Override
		public void onTextChanged(CharSequence arg0, int arg1, int arg2,
				int arg3) {
		}

		@Override
		public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
				int arg3) {
		}

		@Override
		public void afterTextChanged(Editable edt) {
			if (edt.toString().trim().length() != 0) {
				if (Integer.parseInt(edt.toString().trim()) > 999) {
					et_bei.setSelection(et_bei.getText().length());
					MyToast.getToast(getApplicationContext(), "最大倍数为999")
							.show();
					et_bei.setText("999");
				}
				if (Integer.parseInt(edt.toString().trim()) == 0) {
					et_bei.setText("1");
					et_bei.setSelection(et_bei.getText().length());
					MyToast.getToast(getApplicationContext(), "最小为1倍").show();
				}
				if (edt.toString().substring(0, 1).equals("0")) {
					et_bei.setText(edt.toString().subSequence(1,
							edt.toString().length()));
					et_bei.setSelection(0);
				}
			}
			changeTextShow();
			setCursorPosition(et_bei);
		}
	};

	public void setCursorPosition(EditText et) {
		CharSequence text = et.getText();
		if (text instanceof Spannable) {
			Selection.setSelection((Spannable) text, text.length());
		}
	}

	/** 公用Button 点击监听 */
	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		/** 手选 **/
		case R.id.btn_continue_select:
			btn_handClick();
			break;
		/** 返回 **/
		case R.id.btn_back:
			backToPre();
			break;
		/** 机选 **/
		case R.id.btn_automatic_select:
			btn_randomClick();
			break;
		/** 清空 **/
		case R.id.bet_btn_deleteall:
			if (App.list_numbers.size() == 0) {
				MyToast.getToast(BetActivityFC3D.this, "请先选择投注内容").show();
				break;
			}
			dialog.show();
			dialog.setDialogContent("是否清空投注单号码");
			dialog.setDialogResultListener(new ConfirmDialog.DialogResultListener() {

				@Override
				public void getResult(int resultCode) {
					if (1 == resultCode) {// 确定
						App.list_numbers.clear();
						adapter.notifyDataSetChanged();
						changeTextShow();
					}
				}
			});
			break;
		/** 付款 **/
		case R.id.btn_submit:
			commit();
			break;
		/** 发起合买 **/
		case R.id.btn_follow:
			join();
			break;
		case R.id.bet_tv_guize:
			Intent intent = new Intent(BetActivityFC3D.this,
					PlayDescription.class);
			intent.putExtra("type", 0);
			BetActivityFC3D.this.startActivity(intent);
			break;
		}
	}

	/**
	 * 提交购买请求
	 */
	public void commit() {
		if (App.list_numbers.size() > 0) {
			if (App.user != null) {
				// setEnabled(false);
				RequestUtil requestUtil = new RequestUtil(context, false, 0,
						true, "正在支付...") {
					@Override
					public void responseCallback(JSONObject object) {
						// setEnabled(true);
						if (RequestUtil.DEBUG)
							Log.i(TAG, "福彩3D投注支付结果" + object);
						String error = object.optString("error");
						String resultMsg = object.optString("msg");
						if ("0".equals(error)) {
							App.user.setBalance(object
									.optDouble("balance"));
							App.user.setFreeze(object.optDouble("freeze"));
							App.schemeId = object.optInt("schemeids");
							App.lottery.setChaseTaskID(object
									.optInt("chasetaskids"));
							App.list_numbers.clear();

							String SucJinECost = format.format(object
									.optDouble("currentMoeny"));
							String SucCaiJinCost = format.format(object
									.optDouble("currentHandsel"));
							String SuccMoney = format.format(object
									.optDouble("handselMoney"));

							App.totalCount = 0;
							Intent intent = new Intent(getApplicationContext(),
									PaySuccessActivity.class);
							intent.putExtra("paymoney", totalMoney);

							intent.putExtra("currentMoeny", SucJinECost);
							intent.putExtra("currentHandsel", SucCaiJinCost);
							intent.putExtra("handselMoney", SuccMoney);
							context.startActivity(intent);
							App.list_numbers.clear();
							MyGridViewAdapterFC3D.baiSet.clear();
							MyGridViewAdapterFC3D.shiSet.clear();
							MyGridViewAdapterFC3D.geSet.clear();
							MyGridViewAdapterFC3D.daxiaoSet.clear();
							MyGridViewAdapterFC3D.jiouSet.clear();
							App.totalCount = 0;
							finish();
						} else if (error.equals(App.ERROR_MONEY + "")) {
							LotteryUtils.showMoneyLess(context, totalMoney);
						} else if (error.equals("-500")) {
							MyToast.getToast(context, "连接超时").show();
						} else if (error.equals("-111")) {

							MyToast.getToast(BetActivityFC3D.this, resultMsg)
									.show();
						} else {
							Toast.makeText(context, "网络异常，购买失败。请重新点击付款购买。",
									Toast.LENGTH_SHORT).show();
						}
					}

					@Override
					public void responseError(VolleyError error) {
						// setEnabled(true);
						MyToast.getToast(context, "抱歉，支付出现未知错误..").show();
						if (RequestUtil.DEBUG)
							Log.e(TAG, "投注支付报错" + error.getMessage());
					}
				};
				requestUtil.commitBetting(sumCount, totalMoney, isStopChase);
			} else {
				MyToast.getToast(context, "请先登陆").show();
				intent = new Intent(context, LoginActivity.class);
				intent.putExtra("loginType", "bet");
				context.startActivity(intent);
			}
		} else {
			MyToast.getToast(context, "请至少选择一注").show();
			intent = new Intent(context, SelectNumberActivityFC3D.class);
			intent.putExtra("loginType", "bet");
			context.startActivity(intent);
		}
	}

	/** 跳到合买 */
	private void join() {
		if (App.qi > 1) {
			dialog.show();
			dialog.setDialogContent("发起合买时不能追号，是否只追一期并继续发起合买？");
			dialog.setDialogResultListener(new ConfirmDialog.DialogResultListener() {

				@Override
				public void getResult(int resultCode) {
					if (1 == resultCode) {// 确定
						et_qi.setText("1");
						App.qi = 1;
						int total = 0; // 总金额
						for (SelectedNumbers num : App.list_numbers) {
							total += num.getMoney();
						}
						total = total * App.bei;
						intent = new Intent(BetActivityFC3D.this,
								JoinActivity.class);
						intent.putExtra("totalMoney", total + "");
						BetActivityFC3D.this.startActivity(intent);
					}
				}
			});
		} else {
			int total = 0; // 总金额
			for (SelectedNumbers num : App.list_numbers) {
				total += num.getMoney();
			}
			if (total == 0) {
				MyToast.getToast(getApplicationContext(), "您还没有选择号码").show();
				return;
			}
			total = total * App.bei;
			intent = new Intent(BetActivityFC3D.this, JoinActivity.class);
			intent.putExtra("totalMoney", total + "");
			BetActivityFC3D.this.startActivity(intent);
		}
	}

	/** 手选按钮点击事件 */
	private void btn_handClick() {
		clearMyGridViewAdapter();
		Intent intent = new Intent(BetActivityFC3D.this,
				SelectNumberActivityFC3D.class);
		Bundle bundle = new Bundle();
		bundle.putInt("type", MyGridViewAdapterFC3D.playType);
		intent.putExtra("bundle", bundle);
		BetActivityFC3D.this.startActivity(intent);
	}

	/**
	 * 清空所有所选信息
	 */
	private void clearMyGridViewAdapter() {
		if (null != MyGridViewAdapterFC3D.baiSet) {
			MyGridViewAdapterFC3D.baiSet.clear();
		}
		if (null != MyGridViewAdapterFC3D.shiSet) {
			MyGridViewAdapterFC3D.shiSet.clear();
		}
		if (null != MyGridViewAdapterFC3D.geSet) {
			MyGridViewAdapterFC3D.geSet.clear();
		}
		if (null != MyGridViewAdapterFC3D.daxiaoSet) {
			MyGridViewAdapterFC3D.daxiaoSet.clear();
		}
		if (null != MyGridViewAdapterFC3D.jiouSet) {
			MyGridViewAdapterFC3D.jiouSet.clear();
		}
		if (null != MyGridViewAdapterFC3D.zixuanhezhiSet) {
			MyGridViewAdapterFC3D.zixuanhezhiSet.clear();
		}
		if (null != MyGridViewAdapterFC3D.hezhiSet) {
			MyGridViewAdapterFC3D.hezhiSet.clear();
		}
		MyGridViewAdapterFC3D.isCaist = false;
		MyGridViewAdapterFC3D.isTuolaji = false;
		App.totalCount = 0;
	}

	/** 机选按钮点击事件 */
	private void btn_randomClick() {
		// 得到红球的随机数
		String lotteryNumber = "";

		System.out.println(MyGridViewAdapterFC3D.playType);
		if (MyGridViewAdapterFC3D.playType == 601) {
			for (int i = 0; i < 3; i++) {
				lotteryNumber += NumberTools.getRandomNum4(1, 10).get(0) + "|";
			}
		} else if (MyGridViewAdapterFC3D.playType == 602
				|| MyGridViewAdapterFC3D.playType == 6302) {
			if (flag) {
				ArrayList<String> list = NumberTools.getRandomNum4(2, 10);
				list.add(list.get(0));
				Collections.sort(list);
				lotteryNumber = (list.toString().replace(" ", ""))
						.replace(",", "").replace("[", "").replace("]", "");
			} else {
				ArrayList<String> list = NumberTools.getRandomNum4(3, 10);
				Collections.sort(list);
				System.out.println(lotteryNumber);
				lotteryNumber = (list.toString().replace(" ", ""))
						.replace(",", " ").replace("[", " ").replace("]", " ");
			}

		} else if (MyGridViewAdapterFC3D.playType == 603
				|| MyGridViewAdapterFC3D.playType == 6303) {

			ArrayList<String> list = NumberTools.getRandomNum4(4, 10);
			Collections.sort(list);
			lotteryNumber = (list.toString().replace(" ", "")).replace(",", "")
					.replace("[", "").replace("]", "");

		} else if (MyGridViewAdapterFC3D.playType == 604
				|| MyGridViewAdapterFC3D.playType == 6304) {

			ArrayList<String> list = NumberTools.getRandomNum4(2, 10);
			Collections.sort(list);
			lotteryNumber = (list.toString().replace(" ", "")).replace(",", "")
					.replace("[", "").replace("]", "");
		}
		lotteryNumber = lotteryNumber.trim();
		SelectedNumbers numbers = new SelectedNumbers();
		numbers.setPlayType(MyGridViewAdapterFC3D.playType);
		if (lotteryNumber.contains("|")) {
			numbers.setShowLotteryNumber(lotteryNumber.replace("|", " ").trim()
					.replace(" ", ","));
		} else
			numbers.setShowLotteryNumber(lotteryNumber);
		if (MyGridViewAdapterFC3D.playType == 602
				|| MyGridViewAdapterFC3D.playType == 6302) {
			String a[] = lotteryNumber.split(" ");
			String kk = " ";
			for (String anA : a) {
				kk = kk + anA;
			}
			kk.trim();
			String num = NumberTools.lotteryNumberFormatConvert(
					numbers.getPlayType(), kk);
			if (num.contains("|")) {
				num = num.replace("|", ",");
			}
			numbers.setLotteryNumber(num);
		} else {
			String num = lotteryNumber.substring(0, lotteryNumber.length() - 1);
			if (num.contains("|")) {
				num = num.replace("|", ",");
			}
			numbers.setLotteryNumber(num);
		}
		int count;
		if (MyGridViewAdapterFC3D.playType == 604
				|| MyGridViewAdapterFC3D.playType == 6304) {
			numbers.setCount(count = 2); // 设置产生随机数时 的 注数
		} else if (MyGridViewAdapterFC3D.playType == 603
				|| MyGridViewAdapterFC3D.playType == 6303) {
			numbers.setCount(count = 4);
		} else
			numbers.setCount(count = 1);
		numbers.setMoney(2 * count);
		App.list_numbers.add(0, numbers);
		// 刷新Adapter
		adapter.notifyDataSetChanged();
		changeTextShow();
	}

	/** 改变文本的值显示出来 */
	public void changeTextShow() {
		if (et_bei.getText().toString().trim().length() == 0)
			App.bei = 1;
		else
			App.bei = Integer.parseInt(et_bei.getText().toString().trim());

		if (et_qi.getText().toString().trim().length() == 0)
			App.qi = 1;
		else
			App.qi = Integer.parseInt(et_qi.getText().toString().trim());

		sumCount = 0; // 总注数
		for (SelectedNumbers num : App.list_numbers) {
			sumCount += num.getCount();
		}

		if (sumCount != 0) {
			totalMoney = sumCount * 2 * App.bei * App.qi;
			tv_tatol_count.setText(sumCount + "");
			tv_tatol_money.setText(totalMoney + "");
		} else {
			tv_tatol_count.setText("0");
			tv_tatol_money.setText("0");
		}
	}

	/**
	 * 返回
	 */
	public void backToPre() {
		if (App.list_numbers != null && App.list_numbers.size() != 0) {
			dialog.show();
			dialog.setDialogContent("您退出后号码将会清空！");
			dialog.setDialogResultListener(new ConfirmDialog.DialogResultListener() {

				@Override
				public void getResult(int resultCode) {
					if (1 == resultCode) {// 确定
						App.list_numbers.clear();
						MyGridViewAdapterFC3D.baiSet.clear();
						MyGridViewAdapterFC3D.shiSet.clear();
						MyGridViewAdapterFC3D.geSet.clear();
						MyGridViewAdapterFC3D.daxiaoSet.clear();
						MyGridViewAdapterFC3D.jiouSet.clear();
						App.totalCount = 0;
						for (int i = 0; i < App.activityS1.size(); i++) {
							App.activityS1.get(i).finish();
						}
					}
				}
			});
		} else {
			App.list_numbers.clear();
			MyGridViewAdapterFC3D.baiSet.clear();
			MyGridViewAdapterFC3D.shiSet.clear();
			MyGridViewAdapterFC3D.geSet.clear();
			MyGridViewAdapterFC3D.daxiaoSet.clear();
			MyGridViewAdapterFC3D.jiouSet.clear();
			App.totalCount = 0;
			for (int i = 0; i < App.activityS1.size(); i++) {
				App.activityS1.get(i).finish();
			}
		}
	}

	/** 重写返回键事件 */
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			backToPre();
		}
		return super.onKeyDown(keyCode, event);
	}
}
